package com.mhc.haval.service.impl;

import com.mhc.haval.api.dto.AuditQuotaDTO;
import com.mhc.haval.bo.AuditQuotaBO;
import com.mhc.haval.constant.*;
import com.mhc.haval.dal.model.AuditQuotaDO;
import com.mhc.haval.dal.model.DepartmentDO;
import com.mhc.haval.dal.model.StaffDO;
import com.mhc.haval.dal.query.AuditQuotaQuery;
import com.mhc.haval.exception.HavalCommonException;
import com.mhc.haval.manager.AuditQuotaManager;
import com.mhc.haval.manager.DepartmentManager;
import com.mhc.haval.manager.StaffManager;
import com.mhc.haval.service.AuditQuotaService;
import com.subaru.common.entity.BizResult;
import com.subaru.common.query.PageResult;
import com.subaru.common.util.BeanCopierUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.ISDTContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author Churry
 * @create 2017-10-11 11:28
 **/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class AuditQuotaServiceImpl implements AuditQuotaService {

    @Autowired
    AuditQuotaManager auditQuotaManager;
    @Autowired
    DepartmentManager departmentManager;
    @Autowired
    StaffManager staffManager;

    /**
     * 获取审核额度列表
     * @param companyNo
     * @return
     */
    @Override
    public  PageResult<AuditQuotaDO>  getAuditQuotaList(String companyNo,AuditQuotaBO auditQuotaBO) throws HavalCommonException {
        Optional<String> companyNoOptional = Optional.ofNullable(companyNo);
        if (companyNoOptional.isPresent()) {
            log.info("正在获取额度列表...");
            AuditQuotaQuery auditQuotaQuery = new AuditQuotaQuery();
            auditQuotaQuery.setPageNo(auditQuotaBO.getPageNo());
            auditQuotaQuery.setPageSize(auditQuotaBO.getPageSize());
            auditQuotaQuery.createCriteria().andCompanyNoEqualTo(companyNo).andIsDeletedEqualTo(IsDeletedEnum.NORMAL.getCode());
            PageResult<AuditQuotaDO> auditQuotaDOList = auditQuotaManager.selectByQueryWithPage(auditQuotaQuery);
            return auditQuotaDOList;
        } else {
            throw new HavalCommonException(MsgCodeEnum.COMPANY_NO_ISNULL);
        }
    }

    /**
     * 获取审批额度详情
     *
     * @param quotaId
     * @return
     */
    @Override
    public AuditQuotaBO getAuditQuotaDetail(Long quotaId) throws HavalCommonException {
        Optional<Long> quotaIdOptional = Optional.ofNullable(quotaId);
        if (quotaIdOptional.isPresent()) {
        return BeanCopierUtil.convert(auditQuotaManager.selectByPrimaryKey(quotaId), AuditQuotaBO.class);
        } else {
            throw new HavalCommonException(MsgCodeEnum.QUOTA_ID_ISNULL);
        }
    }

    /**
     * 通过员工编号，获取审批额度详情
     *
     * @param staffId
     * @return
     */
    public AuditQuotaBO getAuditQuotaDetailByStaffId(Long staffId) throws HavalCommonException {
        Optional<Long> staffIdOptional = Optional.ofNullable(staffId);
        if (staffIdOptional.isPresent()) {
            AuditQuotaQuery auditQuotaQuery = new AuditQuotaQuery();
            auditQuotaQuery.createCriteria().andStaffIdEqualTo(staffId);
            List<AuditQuotaDO> auditQuotaDOList = auditQuotaManager.selectByQuery(auditQuotaQuery);
            if (auditQuotaDOList.size() > 0) {
                return BeanCopierUtil.convert(auditQuotaDOList.get(0), AuditQuotaBO.class);
            } else {
                return null;
            }
        } else {
            throw new HavalCommonException(MsgCodeEnum.STAFF_ID_ISNULL);
        }
    }

    /**
     * 增加或修改审批额度
     * @param auditQuotaBO
     * @return
     */
    @Override
    public Boolean addOrModifyAuditQuota(AuditQuotaBO auditQuotaBO) throws HavalCommonException {
        AuditQuotaDO auditQuotaDO = BeanCopierUtil.convert(auditQuotaBO, AuditQuotaDO.class);
        auditQuotaDO.setIsDeleted(IsDeletedEnum.NORMAL.getCode());
        //id不为空更新数据
        if (auditQuotaDO.getQuotaId() != null) {
            log.info("正在更新审批额度...");
            auditQuotaManager.updateByPrimaryKeySelective(auditQuotaDO);
        } else {
            log.info("正在校验审批额度是否存在...");
            AuditQuotaQuery auditQuotaQuery = new AuditQuotaQuery();
            if(QuotaTypeEnum.DEPT_QYOTA.getCode().equals(auditQuotaBO.getQuotaType())){
                /**当选择部门额度**/
                auditQuotaBO.setDeptName(QuotaTypeEnum.DEPT_QYOTA.getName());
                auditQuotaQuery.createCriteria().andDeptNameEqualTo(QuotaTypeEnum.DEPT_QYOTA.getName()).andIsDeletedEqualTo(IsDeletedEnum.NORMAL.getCode());
            }else {
                /**当选择员工额度**/
                auditQuotaQuery.createCriteria().andStaffIdEqualTo(auditQuotaBO.getStaffId()).andIsDeletedEqualTo(IsDeletedEnum.NORMAL.getCode());
            }
            List<AuditQuotaDO> auditQuotaList = auditQuotaManager.selectByQuery(auditQuotaQuery);
            if(!auditQuotaList.isEmpty()){
                throw new HavalCommonException(MsgCodeEnum.DATA_EXISTS);
            }
            log.info("正在新增审批额度...");
            auditQuotaManager.insertSelective(auditQuotaDO);
        }
        log.info("审批额度设置完成...");
        return true;
    }

    /**
     * 删除审批额度
     * @param quotaId
     * @return
     * @throws HavalCommonException
     */
    @Override
    public Boolean deleteAuditQuota(Long quotaId) throws HavalCommonException{
        log.info("正在删除审批额度...");
        AuditQuotaDO auditQuotaDO = new AuditQuotaDO();
        auditQuotaDO.setQuotaId(quotaId);
        auditQuotaDO.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        auditQuotaManager.updateByPrimaryKeySelective(auditQuotaDO);
        log.info("删除审批额度完成...");
        return true;
    }

}
